package com.woniuxy.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Merchants;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.entity.Warehouse;
import com.woniuxy.service.WarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Collection;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/warehouse")
@Api(tags = "仓库接口")
public class WarehouseController {
    @Autowired
    WarehouseService warehouseService;

    @GetMapping("/findAll")
    @ApiOperation(value = "获取仓库详情")
    public ResponseEntity findAll() {
        return warehouseService.findAll();
    }

    @GetMapping("/findAll/{currentPage}/{pageSize}")
    @ApiOperation(value = "获取仓库分页详情")
    public ResponseEntity findAll(@PathVariable int currentPage, @PathVariable int pageSize) {
        return warehouseService.findAll(currentPage, pageSize);
    }

    @GetMapping("/findByName/{currentPage}/{pageSize}/{name}")
    @ApiOperation(value = "模糊查询")
    public ResponseEntity findByName( @PathVariable int currentPage,@PathVariable int pageSize, @PathVariable String name) {
        String decode = URLDecoder.decode(name);
        ResponseEntity all = warehouseService.findByName(currentPage, pageSize, decode);
        PageInfo info = new PageInfo<Warehouse>((List<Warehouse>) all.getData());
        all.setData(info);
        return all;
    }

    @GetMapping("/findOne/{id}")
    @ApiOperation(value = "获取仓库中的一个仓库数据")
    public ResponseEntity findOne(@ApiParam(name = "id", value = "仓库id") @PathVariable String id) {
        return warehouseService.findOne(id);
    }

    @PostMapping("/addOne")
    @ApiOperation(value = "新增一个仓库")
    public ResponseEntity addOne(@ApiParam(name = "warehouse", value = "新增的仓库") @RequestBody Warehouse warehouse) {
        return warehouseService.addOne(warehouse);
    }

    @PutMapping("/update")
    @ApiOperation(value = "更新仓库")
    public ResponseEntity updateOne(@ApiParam(name = "warehouse", value = "修改仓库的描述") @RequestBody Warehouse warehouse) {
        return warehouseService.updateOne(warehouse);
    }

    @PutMapping("/delete/{id}")
    @ApiOperation(value = "删除一个仓库")
    public ResponseEntity deleteOne(@ApiParam(name = "id", value = "仓库id") @PathVariable String id) {
        return warehouseService.deleteOne(id);
    }

    @PutMapping("/deleteSome")
    @ApiOperation(value = "删除多个仓库")
    public ResponseEntity deleteSome(@ApiParam(name = "id", value = "仓库id") @RequestBody int[] id) {
        return warehouseService.deleteSome(id);
    }

    @GetMapping("/findByCode/{warehouse_id}")
    @ApiOperation(value = "根据编码查询产品数据")
    public ResponseEntity findByCode(@ApiParam(name = "warehouse_id", value = "仓库编码") @PathVariable String warehouse_id) {
        return warehouseService.findByCode(warehouse_id);
    }

    @GetMapping("/findByWname/{name}")
    @ApiOperation(value = "根据名称查询产品数据")
    public ResponseEntity findByWname(@ApiParam(name = "warehousename", value = "仓库名称") @PathVariable String name) {
        String newName = null;
        try {
            newName = URLDecoder.decode(name, "utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return warehouseService.findByWname(newName);
    }

    @PostMapping("/import")
    @ApiOperation(value = "从excel导入")
    public ResponseEntity importExcel(@ApiParam(name = "file", value = "excel文件") @RequestPart("file") MultipartFile file) {
        List<Warehouse> list = null;
        try {
            list = EasyExcel.read(file.getInputStream())
                    .head(Warehouse.class)
                    .sheet()
                    .doReadSync();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return warehouseService.importExcel(list);
    }

    @GetMapping("/export")
    @ApiOperation(value = "导出excel")
    public void exportExcel(HttpServletResponse response) throws IOException {
        // 这里URLEncoder.encode可以防止中文乱码，所有通过后端的文件下载都可以如此处理
        String fileName = URLEncoder.encode("仓库列表.xlsx", "UTF-8");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf8");
        //建议加上该段，否则可能会出现前端无法获取Content-disposition
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName);
        EasyExcel.write(response.getOutputStream(), Warehouse.class).sheet("模板")
                .doWrite((Collection<?>) warehouseService.findAll().getData());
    }
}
